<template>
  <div class="user-container">
    <div class="header">
      <!------------------------------------------- 未登录------------------------------  -->
      <div class="header-login" v-if="!$store.state.user">
        <ul class="log">
          <li>
            <img src="../assets/img/mobile.png" />
          </li>
          <li @click="login">
            <span>登录</span>/
            <span>注册</span>
          </li>
        </ul>
      </div>
      <!-------------------------------------- 已登录 --------------------------------------->
      <div class="header-login-on" v-else>
        <div class="base-info">
          <div class="left">
            <van-image round fit="cover" width="80px" height="80px" :src="userInfo.photo" />
            <span class="txt">{{userInfo.name}}</span>
          </div>
          <div class="righr">
            <van-button round size="mini" @click="$router.push({path:'/users/profile'})">编辑资料</van-button>
          </div>
        </div>
        <div class="data-stats">
          <div class="data-item">
            <div class="count">{{userInfo.art_count}}</div>
            <div class="text">头条</div>
          </div>
          <div class="data-item">
            <div class="count">{{userInfo.like_count}}</div>
            <div class="text">关注</div>
          </div>
          <div class="data-item">
            <div class="count">{{userInfo.fans_count}}</div>
            <div class="text">粉丝</div>
          </div>
          <div class="data-item">
            <div class="count">{{userInfo.follow_count}}</div>
            <div class="text">获赞</div>
          </div>
        </div>
      </div>
    </div>
    <!---------------------------------- 收藏 历史 ----------------------------------------->
    <div class="user-nav">
      <van-grid :column-num="2">
        <van-grid-item icon="star-o" text="收藏" />
        <van-grid-item icon="clock-o" text="历史" />
      </van-grid>
    </div>
    <!----------------------------------- 第三栏 ------------------------------------------------->
    <div class="user-list">
      <van-cell title="消息通知" is-link />
      <van-cell title="小智同学" is-link />
    </div>
    <!----------------------------- 退出登录 ---------------------------------------->
    <van-button class="exit" block @click="outLogin" v-if="$store.state.user">退出登录</van-button>
    <!--------------------- 脱出弹出窗 --------------------->
  </div>
</template>
           
<script>
import { mapState } from "vuex";
import { SET_TOKEN } from "@/utils/storage.js";
import { getUserInfo_api } from "@/api/login/index";
// 弹出窗=---------------
import { Dialog } from 'vant';
export default {
  data() {
    return {
      //用户信息列表----------------------------
      userInfo: {}
    };
  },
  created() {
    //如果用户登录，就有token，才能加载页面
    if (this.user) {
      this.toUserInfo();
    }
  },
  computed: {
    //获取vuex仓库 user的token值------
    ...mapState(["user"])
  },
  methods: {
    //获取用户信息---------------------
    async toUserInfo() {
      try {
        const { data: res } = await getUserInfo_api();
        // console.log(res, 66655);
        this.userInfo = res.data;
        SET_TOKEN('userInfo',this.userInfo)
      } catch (err) {
        this.$toast.fail("获取失败");
      }
    },
    //退出登录-----------------------
    outLogin() {
      Dialog.confirm({
        message: "确认退出吗"
      })
        .then(() => {
          //删除user里的token
          // SET_TOKEN('TOUTIAO_USER',null)
          this.$store.commit("SET_TOKEN", null)
          // this.$router.push('/login')
        })
        .catch(() => {
          // on cancel
           console.log('取消执行这里')
        });
    },
    login() {
      this.$router.push("/login");
    }
  }
};
</script>
<style scoped lang="less">
.user-container {
  background-color: #f5f7f9;
  .header-login {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 360px;
    background: url("../assets/img/banner.png") no-repeat;
    background-size: cover;
    .log {
      li {
        text-align: center;
        color: #fff;
        font-size: 35px;
        margin-top: 10px;
        img {
          width: 132px;
          height: 132px;
        }
      }
    }
  }
  /deep/.van-grid {
    /deep/.van-grid-item__text {
      margin: 0;
      font-size: 30px;
    }
    .van-icon-star-o {
      color: #eb5253;
    }
    .van-icon-clock-o {
      color: rgb(255, 157, 29);
    }
  }
  .header-login-on {
    background: url("../assets/img/banner.png") no-repeat center;
    .base-info {
      display: flex;
      justify-content: space-between;
      align-items: center;
      width: 100%;
      height: 231px;
      padding: 76px 32px 23px;
      font-size: 30px;
      .left {
        display: flex;
        align-items: center;
        .txt {
          margin-left: 20px;
          color: #fff;
          font-size: 30px;
        }
      }
      .right {
        van-buutton {
          background-color: #fff;
        }
      }
    }
    .data-stats {
      width: 100%;
      display: flex;
      .data-item {
        flex: 1;
        height: 130px;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        .count,
        .text {
          font-size: 20px;
          color: #fff;
        }
        .count {
          font-size: 40px;
        }
      }
    }
  }
  .exit {
    margin-top: 10px;
  }
  /deep/.van-button__text {
    color: #eb5253;
  }
}
</style>